package com.ruoyi.web.controller.business;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.business.domain.SysAppUser;
import com.ruoyi.business.domain.dto.UserAndTokenDto;
import com.ruoyi.business.domain.vo.GetOpenIdVo;
import com.ruoyi.business.domain.vo.GetPhoneVo;
import com.ruoyi.business.domain.vo.InitUserInfoVo;
import com.ruoyi.business.domain.vo.ReqGetPhoneNumberVO;
import com.ruoyi.business.service.ISysAppUserService;
import com.ruoyi.business.service.WXAppletService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.domain.SmsCodeAuthenticationToken;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.util.AliMessageUtil;
import com.ruoyi.util.WXBizDataCrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Larry
 * @description
 */
@Api(tags = {"外部调用"})
@RequestMapping("/out")
@RestController
public class OutServiceController {
    @Autowired
    private WXAppletService wxAppletService;
    @Autowired
    private ISysAppUserService userService;
    private static final String PASSWORD = "abc_123*!2W";

    /**
     * 小程序通过code取openId、unionId
     *
     * @param code 授权code
     * @author: diandong滕玉静
     * @date: 2021-12-30
     */
    @GetMapping("/getOpenIdByCode")
    @ApiOperation(value = "app小程序通过code取openId、unionId")
    public R<GetOpenIdVo> getOpenIdByCode(@RequestParam("code") String code) {
        GetOpenIdVo map = wxAppletService.getOpenIdByCode(code);
        return R.ok(map);
    }

    /**
     * 小程序授权登录(-系统登录逻辑)
     *
     * @param openid 小程序openid
     * @date: 2022-03-25
     */
    @GetMapping("/appletLogin")
    @ApiOperation(value = "app小程序授权登录(-系统登录逻辑)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", dataTypeClass = String.class),
    })
    public R<UserAndTokenDto> appletLogin(@RequestParam("openid") String openid,String phone) {
        //小程序授权登录(-系统登录逻辑)
        UserAndTokenDto user = wxAppletService.appletLogin(openid,phone);
        return R.ok(user);
    }

    @PostMapping("/initUserInfo")
    @ApiOperation("app绑定用户信息  当用户头像为空时需要拉取授权 传入此接口  后续更新也使用此接口")
    public AjaxResult initUserInfo(@RequestBody InitUserInfoVo initUserInfoVo) {
        SysAppUser sysAppUser = BeanUtil.copyProperties(initUserInfoVo, SysAppUser.class);
        sysAppUser.setAppUserId(SecurityUtils.getUserId());
        userService.updateById(sysAppUser);
        return AjaxResult.success();
    }







}
